/<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运单号" prop="shippingNoteNumber">
        <el-input
          v-model="queryParams.shippingNoteNumber"
          placeholder="请输入运单号"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机名"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="vehicleNumber">
        <el-input
          v-model="queryParams.vehicleNumber"
          placeholder="请输入车牌号"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="driverPhone">
        <el-input
          v-model="queryParams.driverPhone"
          placeholder="请输入手机号"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="装货地址" prop="placeOfLoading">
        <el-input
          v-model="queryParams.placeOfLoading"
          placeholder="请输入装货地址"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卸货地址" prop="goodsReceiptPlace">
        <el-input
          v-model="queryParams.goodsReceiptPlace"
          placeholder="请输入卸货地址"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择运单解除状态" clearable size="mini">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="primary"-->
          <!--plain-->
          <!--icon="el-icon-plus"-->
          <!--size="mini"-->
          <!--@click="handleAdd"-->
          <!--v-hasPermi="['tws:secure:add']"-->
        <!--&gt;新增</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['tws:secure:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--</el-row>-->

    <el-table v-loading="loading" :data="secureList" @selection-change="handleSelectionChange" border>
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="ID" align="center" prop="id" />-->
      <!--<el-table-column label="订单ID" align="center" prop="orderId" />-->
      <el-table-column label="运单号" align="center" prop="shippingNoteNumber" show-overflow-tooltip/>
      <el-table-column label="司机名" align="center" prop="driverName" width="150"/>
      <el-table-column label="车牌号" align="center" prop="vehicleNumber" width="150"/>
      <el-table-column label="司机手机号" align="center" prop="driverPhone" />
      <el-table-column label="运费金额" align="center" prop="totalMonetaryAmount" width="150"/>
      <el-table-column label="装货地址" align="center" prop="placeOfLoading" show-overflow-tooltip />
      <el-table-column label="卸货地址" align="center" prop="goodsReceiptPlace" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="解除原因" align="center" prop="secureReason" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['tws:secure:edit']"
          >查看订单</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: limegreen"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tws:secure:edit']"
            v-if="scope.row.status == 0"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: red"
            @click="reject(scope.row)"
            v-hasPermi="['tws:secure:remove']"
            v-if="scope.row.status == 0"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改解除运单申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form :model="driverParams" ref="driverForm" :inline="true" label-width="68px">
        <el-form-item label="姓名" prop="driverName">
          <el-input
            v-model="driverParams.driverName"
            placeholder="请输入姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input
            v-model="driverParams.telephone"
            placeholder="请输入手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleDriverQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetDriverQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="openAdd = true"
          >添加司机</el-button>
        </el-col>
      </el-row>

          <el-table v-loading="driverLoading" :data="driverList" border>
            <el-table-column label="姓名" align="center" prop="driverName" />
            <el-table-column label="手机号" align="center" prop="telephone" width="115" />
            <el-table-column label="准驾车型" align="center" prop="vehicleClass" />
            <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
            <el-table-column label="认证状态" align="center" prop="isCert" :formatter="isCertFormat" />
            <!--<el-table-column label="状态" align="center" width="140" >-->
              <!--<template slot-scope="scope">-->
                <!--<el-switch-->
                  <!--v-model="scope.row.state"-->
                  <!--active-color="#13ce66"-->
                  <!--inactive-color="#ff4949"-->
                  <!--active-value="1"-->
                  <!--inactive-value="2"-->
                  <!--active-text="正常"-->
                  <!--inactive-text="禁用" disabled>-->
                <!--</el-switch>-->
              <!--</template>-->
            <!--</el-table-column>-->
            <el-table-column label="操作" align="center" fixed="right">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="chooseDriver(scope.row)"
                >选择</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="driverTotal>0"
            :total="driverTotal"
            :page.sync="driverParams.current"
            :limit.sync="driverParams.size"
            @pagination="getDriverList"
          />

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="rejectOpen" width="500px" append-to-body>
      <el-form :model="rejectForm" ref="rejectForm" >
        <el-form-item label="驳回原因" prop="rejectReason">
          <el-input type="textarea" placeholder="请输入驳回原因" v-model="rejectForm.rejectReason"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="rejectDisabled" @click="submitReject">确 定</el-button>
        <el-button @click="rejectCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 物流公司添加司机对话框 -->
    <el-dialog title="添加司机" :visible.sync="openAdd" width="300px" append-to-body :close-on-click-modal="false">
      <driver-add @driverAdd="driverAdd" />
    </el-dialog>
  </div>
</template>

<script>
import { listSecure, getSecure, delSecure, addSecure, updateSecure, rejectSecure } from "@/api/tws/secure";
import { listDriver, listDriverAll } from "@/api/tws/driver";
import DriverAdd from '@/components/DriverAdd/index';


export default {
  name: "Secure",
  components: {
    DriverAdd
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      driverLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 禁用弹窗新增或修改的确定按钮
      disabled: false,
      rejectDisabled: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 认证状态字典
      isCertOptions: [],
      // 性别字典
      sexOptions: [],
      // 运单解除状态字典
      statusOptions: [],
      // 司机条数
      driverTotal: 0,
      // 解除运单申请表格数据
      secureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      rejectOpen: false,
      openAdd:false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        orderId: null,
        shippingNoteNumber: null,
        driverName: null,
        vehicleNumber: null,
        driverPhone: null,
        totalMonetaryAmount: null,
        secureReason: null,
        placeOfLoading: null,
        goodsReceiptPlace: null,
      },
      driverParams: {
        current: 1,
        size: 10,
      },
      // 表单参数
      form: {},
      // 驳回表单
      rejectForm: {},
      // 表单校验
      rules: {
      },
      // 选择替换司机
      driverList:[],
    };
  },
  created() {
    this.getDict();
    this.getList();
  },
  methods: {
    /** 查询字典 */
    getDict() {
      // 认证状态字典
      this.getDicts("is_cert").then(response => {
        this.isCertOptions = response.data;
      });
      // 性别字典
      this.getDicts("sys_user_sex").then(response => {
        this.sexOptions = response.data;
      });
      // 运单解除状态字典
      this.getDicts("order_secure_status").then(response => {
        this.statusOptions = response.data;
      })
    },
    /** 查询解除运单申请列表 */
    getList() {
      this.loading = true;
      listSecure(this.queryParams).then(response => {
        this.secureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 查看
    handleView(row) {
      this.$router.push({ path: '/order/orderDetail', query: { orderId: row.orderId }, replace: true });
    },

    /** 查询司机信息列表 */
    getDriverList() {
      this.driverLoading = true;
      // 物流公司登陆只查常用司机
      if(this.userType == this.$store.state.constants.userType_company) {
        listDriver(this.driverParams).then(response => {
          this.driverList = response.rows;
          this.driverTotal = response.total;
          this.driverLoading = false;
        });
      } else {
        listDriverAll(this.driverParams).then(response => {
          this.driverList = response.rows;
          this.driverTotal = response.total;
          this.driverLoading = false;
        });
      }
    },

    //选择司机
    chooseDriver(row){
      let that = this;
      let orderData ={};
      orderData.id = this.form.orderId;
      orderData.driverId = row.id;
      orderData.secureId = this.form.id;
      orderData.driverLicense = row.driverLicense;
      orderData.driverName = row.driverName;
      orderData.driverPhone = row.telephone;
      this.$confirm('确认后将由:'+orderData.driverName+' 接手本单',{
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        updateSecure(orderData).then(res =>{
          that.$modal.msgSuccess("申请成功!");
          that.getList();
          that.open = false;
        })
      })
    },

    //确认驳回
    submitReject(){
      let that = this;
      this.rejectDisabled = true;
      rejectSecure(this.rejectForm).then(res =>{
        that.$modal.msgSuccess("已驳回");
        that.rejectDisabled =false;
        that.rejectOpen = false;
        that.getList();
      })
    },

    // 驳回
    reject(row){
      this.rejectReset();
      const id = row.id;
      getSecure(id).then(res =>{
        this.rejectForm = res.data;
        this.title = "驳回";
        this.rejectOpen = true;
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    rejectCancel(){
      this.rejectOpen = false;
      this.rejectReset();
    },

    // 添加司机的回调
    driverAdd(query) {
      this.getList();
      this.openAdd = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        shippingNoteNumber: null,
        driverName: null,
        vehicleNumber: null,
        driverPhone: null,
        totalMonetaryAmount: null,
        secureReason: null,
        placeOfLoading: null,
        goodsReceiptPlace: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    rejectReset(){
      this.rejectForm = {
        id: null,
        rejectReason: null,
      };
      this.resetForm("rejectForm");
    },

    /** 司机搜索按钮操作 */
    handleDriverQuery(){
      this.driverParams.current = 1
      this.getDriverList();
    },
    /** 司机搜索重置操作 */
    resetDriverQuery(){
      this.resetForm("driverForm")
      this.handleDriverQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加解除运单申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSecure(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "转单";
        this.getDriverList();
      });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.disabled = true;
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateSecure(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.disabled =false;
    //           this.open = false;
    //           this.getList();
    //         }).catch(err => {
    //           this.disabled =false;
    //         })
    //       }
    //     } else {
    //       this.disabled = false;
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleReject(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除解除运单申请编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSecure(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(err => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/secure/export', {
        ...this.queryParams
      }, `tws_secure.xlsx`)
    },
    // 认证状态字典翻译
    isCertFormat(row, column) {
      return this.selectDictLabel(this.isCertOptions, row.isCert);
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 运单解除状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
  }
};
</script>
